package sc
{
	import flash.events.*;
	import flash.filesystem.*;
	
	import flexlib.events.SuperTabEvent;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
		

	
	public class App
	{
		private static var closeOK:Boolean = false;
		
		
		public function App()
		{
		}
		public static function invokeHandler(invokeEvent:InvokeEvent):void {
		if((invokeEvent.currentDirectory != null)&&(invokeEvent.arguments.length > 0)) {
			// the user want's to open a .texy file
		    var dir:* = invokeEvent.currentDirectory;
		    var currentFile:* = dir.resolvePath(invokeEvent.arguments[0]) as File;
			var stream:FileStream = new FileStream();
			stream.openAsync(currentFile, FileMode.READ);
			stream.addEventListener(Event.COMPLETE, $.file.fileReadHandler);
			stream.addEventListener(IOErrorEvent.IO_ERROR, $.file.readIOErrorHandler);
			$.file.dataChanged = false;
			
			var nam:String = currentFile.name;
			$.file.newDoc();
			var hell:texy_editor_tab = $.main.tabCont.selectedChild as  texy_editor_tab;
			
			hell.label = nam;
		}
	}
	
	

		/**
		* Called as the event handler for the applicationComplete event of the Application. This method
		* sets the position and size of the main window.
		*/
		public static function appCompleteHandler():void 
		{
			$.file = new Files();
			//var menu:Menue = new Menue();
			new Menue();
			$.main.tabCont.addEventListener(SuperTabEvent.TAB_CLOSE, App.tabCloseHandler);
			
			
			$.main.tabCont.selectedIndex = 0;
			
			try
			{
				$.file.open((new File('app:/assets/tutorial.texy')));
			}
			catch(e)
			{
				
			}
			
			
			// Uncoment this line when the update framework stops ruining the user interface
			var update:Update = new Update();
			
				
	
		}
		
		
		public static function sendit(event:MouseEvent):void {
			var obj:Object;
			try {
				obj = $.main.prefs.constructQuery();
			} catch(e:TypeError) {
				obj = {};
			}
			obj.text = getInp();
			var hell:texy_editor_tab = $.main.tabCont.selectedChild as  texy_editor_tab;
			hell.prog.source = $.main.shit;
			hell.prog.indeterminate = true;
			hell.prog.visible = true;
			hell.sub.enabled = false;
			$.main.shit.send(obj);

			trace('clicked');
		}
		
		public static function getInp():String {
			var hell:texy_editor_tab = $.main.tabCont.selectedChild as  texy_editor_tab;
			
			return hell.input;
		}
		public static function tabCloseHandler(event:SuperTabEvent):void {
			trace('tab closed');
			var hell:texy_editor_tab = $.main.tabCont.getChildAt(event.tabIndex) as  texy_editor_tab;
			if(hell.dataChanged && !App.closeOK) {
				Alert.show("Save before closing?", "Close", mx.controls.Alert.YES| mx.controls.Alert.NO | Alert.CANCEL, $.main, App.alertListener, null, Alert.OK);
				event.preventDefault();
			} else if(App.closeOK) {
				App.closeOK = false;
			}
			if($.main.tabCont.numChildren == 1) $.file.newDoc();			
		}
		
		public static function alertListener(eventObj:CloseEvent):void {
	        // Check to see if the OK button was pressed.
	        var hell:texy_editor_tab = $.main.tabCont.selectedChild as  texy_editor_tab;
	        if (eventObj.detail==Alert.YES) {
	        	hell.defaultDirectory = $.file.defaultDirectory;
	        	hell.saveFile();
	            //closeOK = true;
	            //tabCont.removeChild(hell);
	        } else if(eventObj.detail==Alert.NO) {
	        	$.main.tabCont.removeChild(hell);
	        }
	    }
	    
	    public static function closingHandler(event:Event):void {
	    	$.main.prefs.savePrefs();
	    }
	}
}